<template>
  <div id="app">
    <el-container class="home-container">
      <!-- 头部区 -->
      <el-header>
        <div>
          <!-- <img src="../assets/head.png" style="width:60px"> -->
          <span>后台管理系统</span>
        </div>
        <el-button type="danger" @click="logout">退出</el-button>
      </el-header>

      <!-- 主体区 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class=" toggle-button" @click="toggleCollapse">|||</div>
          <el-menu class="el-menu-vertical-demo" background-color="#4b4b4b" text-color="#fff" unique-opened router
            :collapse="isCollapse" :collapse-transition="false" default-active="/users">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
                @click="saveNavState('/'+subItem.path)" :default-active="activePath">
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容主题 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  created () {
    this.getMenuList();
    // this.activePath = window.sessionStorage.getItem('activePath', activePath)
  },
  data () {
    return {
      //菜单数据
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-wo',
        '103': 'iconfont icon-icon-mima',
        '102': 'iconfont icon-biji',
        '101': 'iconfont icon-gouwuchetianchong',
        '145': 'iconfont icon-renwu',
      },
      //是否折叠
      isCollapse: false,
      //激活状态
      activePath: '',
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.menulist = res.data
    },
    // 菜单折叠和展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    //保存激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style scoped>
.el-header {
  background-color: #6d6d6d;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-footer {
  background-color: #6d6d6d;
  color: #333;
}
.el-aside {
  background-color: #4b4b4b;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

.home-container {
  height: 100%;
}
.el-header > div {
  display: flex;
  align-items: center;
}
.el-header > div > span {
  margin-left: 15px;
}
[class*="el-col-"] {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.el-col-12 {
  width: 100%;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  background-color: aquamarine;
  text-align: center;
  color: #fff;
  line-height: 25px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>